package com.boang.stream;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

/*
    @author:sa
    @date:2024/12/2 15:39
*/
public class StreamDemo {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student("zhangsan", 20, 89));
        list.add(new Student("lisi", 30, 99));
        list.add(new Student("wangwu", 25, 79));
        list.add(new Student("zhaoliu", 60, 63));
        list.add(new Student("degang", 45, 80));
        list.add(new Student("shaobing", 33, 72));
        list.add(new Student("yunpeng", 36, 60));
        Stream<Student> stream = list.stream();
        long count=stream.count();
        System.out.println("count="+count);
        stream.forEach(new Consumer<Student>() {
            @Override
            public void accept(Student student) {
                System.out.println("student="+student);
            }
        });
        stream.forEach(System.out::println);
        list.stream().filter(new Predicate<Student>() {
            @Override
            public boolean test(Student student) {
                return student.name.startsWith("z");
            }
        }).forEach(System.out::println);
        stream.filter(student->student.name.startsWith("z"));
        stream.sorted(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.score- o2.score;
            }
        }).forEach(System.out::println);
        stream.sorted((o1, o2) -> o1.score- o2.score).forEach(System.out::println);
        List<String> stringList=list.stream().map(new Function<Student, String>() {
            @Override
            public String apply(Student student) {
                return student.name;
            }
        }).toList();
stringList.stream().forEach(System.out::println);
        stream.map(Student::toString);
    }
}